<?php

require_once APPPATH . "/controllers/BasePriv.php";
require_once APPPATH . "/libraries/phpqrcode/phpqrcode.php";
require_once APPPATH . "/libraries/aws/aws-autoloader.php";
require_once APPPATH . "/libraries/Tools.php";

use Aws\S3\S3Client;

class Type extends BasePriv
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', "url"));
        $this->load->model("Host_type_model", "table");
        $this->load->model("Host_user_material_model", "user_material");
        $this->load->model("Host_video_model", "video");
        $this->detail_msg = "detail_msg";//操作详细表
    }

    /**
     * 分类主页
     */
    public function index()
    {
        //查询所有分类
        $arr = [];
        $res = $this->table->type($arr);
        //获取标签
        $select = $this->table->biaoqian();
        foreach ($res['row'] as $k => &$v) {
            $v['select'] = $select;
        }
        $data['type'] = $res;
        $this->layout("type/type", $data);
    }

    /**
     * 分类编辑
     */
    public function edit_type()
    {
        //查询当前分类
        $row = $this->table->getOneType($_GET['id']);

        //获取标签
        $select = $this->table->biaoqian();

        $data['select'] = $select;
        $data['row'] = $row;
        $this->layout("type/edit_type", $data);
    }

    /**
     * 分类删除
     */
    public function del_type()
    {
        $id = $_POST['id'];
        // 话题下是否有内容

        $count = $this->user_material->getTotal(['join_type' => $id, 'del1' => 2]);
        $count2 = $this->video->getTotal(['video_join_type' => $id, 'status' => 2]);
        if ($count != 0 || $count2 != 0) {
            response(-1, "当前话题分类存在内容，无法删除；如要删除，请先删除话题下的全部内容");
        }

        $status = $this->table->delete(['id' => $id]);
        if ($status > 0) {
            response(0, "删除成功");
        } else {
            response(-1, "删除失败");
        }
    }

    /**
     * 新增分类
     */
    public function save_type()
    {
        $hide_name = $_POST['hide_name'];
        $hide_city = $_POST['hide_city'];
        $hide_password = $_POST['hide_password'];
        if (empty($hide_name)) {
            response(-1, "分类名称不能为空");
        }
        if (empty($hide_password)) {
            $hide_password = 999999999;
        }
        //先去查询下是否已经新建了这个分类
        $is_save1 = $this->table->one("type", ['test_name' => $hide_name]);
        if ($is_save1) {
            response(-1, "该分类已经存在");
        }
        $is_save2 = $this->table->one("type", ['name' => $hide_name]);
        if ($is_save2) {
            response(-1, "该分类已经存在");
        }
        if (mb_strlen($hide_name) > 5) {
            response(-1, "分类最大不能超过5个字");
        }
        $data['test_name'] = $hide_name;
        $data['test_sort'] = $hide_password;
        $data['test_biaoqian'] = $hide_city;
        $data['add_time'] = date("Y-m-d H:i:s", time());
        $data['update_time'] = date("Y-m-d H:i:s", time());
        //添加数据
        $res = $this->table->add("type", $data);
        if ($res) {
            response(0, "添加成功");
        } else {
            response(-1, "添加失败");
        }
    }

    /**
     * 已新增分类上线
     */
    public function up_button()
    {
        $data['test_online'] = 2;
        $data['update_time'] = date("Y-m-d H:i:s", time());
        $where['id'] = $_POST['id'];
        $res = $this->table->update("type", $data, $where);
        if ($res) {
            response(0, "上线成功");
        } else {
            response(-1, "上线失败");
        }
    }

    /**
     * 已新增分类下线
     */
    public function down_button()
    {

        $data['test_online'] = 1;
        $data['test_sort'] = 999999999;
        $data['update_time'] = date("Y-m-d H:i:s", time());
        $where['id'] = $_POST['id'];
        $res = $this->table->update("type", $data, $where);
        if ($res) {
            response(0, "下线成功");
        } else {
            response(-1, "下线失败");
        }
    }

    /**
     * 已新增分类保存
     */
    public function save_button()
    {
        if (empty($_POST['test_name'])) {
            response(-1, "分类名称不能为空");
        }
        // if (!intval($_POST['test_sort'])) {
        //     response(-1, "排序值不是数字");
        // }
        if (empty($_POST['test_online']) || $_POST['test_online'] < 0 || $_POST['test_online'] > 2) {
            response(-1, "上下线状态错误");
        }
        if (empty($_POST['test_sort']) || $_POST['test_sort'] == 0) {
            $_POST['test_sort'] = 999999999;
        } else if ($_POST['test_sort'] < 0 || $_POST['test_sort'] > 100) {
            response(-1, "排序值不合法");
        }
        if (mb_strlen($_POST['test_name']) > 6) {
            response(-1, "分类最大不能超过6个字");
        }
        //先去查询下是否已经新建了这个分类
        $is_save1 = $this->table->one_save(['test_name' => $_POST['test_name']], $_POST['id']);
        if ($is_save1) {
            response(-1, "该分类已经存在");
        }
        $is_save2 = $this->table->one_save(['name' => $_POST['test_name']], $_POST['id']);
        if ($is_save2) {
            response(-1, "该分类已经存在");
        }
        //先去修改排序
        $res1 = $this->table->update_sort($_POST['test_sort'], $_POST['id'], $is_save2['test_sort']);
        if ($res1) {
            $data['test_name'] = $_POST['test_name'];
            $data['test_online'] = $_POST['test_online'];
            $data['test_biaoqian'] = $_POST['test_biaoqian'];
            $data['update_time'] = date("Y-m-d H:i:s", time());
            $where['id'] = $_POST['id'];
            $res = $this->table->update("type", $data, $where);
            if ($res) {
                response(0, "保存成功");
            } else {
                response(-1, "修改失败2");
            }
        } else {
            response(-1, "修改失败1");
        }
    }

    /**
     * 获取标签
     */
    public function biaoqian()
    {
        $biaoqian = $this->table->biaoqian();
        if ($biaoqian) {
            response(0, "获取成功", $biaoqian);
        } else {
            response(-1, "获取失败", $biaoqian);
        }
    }

    /**
     * 发布分类
     */
    public function fabu()
    {
        $fa = $_POST['fa'];
        if ($fa == "not_test") {
            $res = $this->table->fabu();

            if ($res) {
                //拼接数据入库
                $data['user_id'] = $this->userInfo['role_id'];
                $data['user_name'] = $this->userInfo['name'];
                $data['detail_msg'] = json_encode($res);
                $data['add_time'] = date("Y-m-d H:i:s", time());
                $this->table->add($this->detail_msg, $data);
                response(0, "发布成功");
            } else {
                response(-1, "发布失败");
            }
        } else {
            response(-1, "发布失败");
        }
    }
}


